package com.yzxb.messystem.controller;

import com.yzxb.messystem.entity.Product;
import com.yzxb.messystem.exception.ResourceNotFoundException;
import com.yzxb.messystem.service.ProductService;
import com.yzxb.messystem.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/products")
public class ProductController {
    
    @Autowired
    private ProductService productService;
    
    // 获取所有产品
    @GetMapping
    public ResponseResult<List<Product>> getAllProducts() {
        List<Product> products = productService.getAllProducts();
        return ResponseResult.success("获取产品列表成功", products);
    }
    
    // 根据ID获取产品
    @GetMapping("/{id}")
    public ResponseResult<Product> getProductById(@PathVariable Long id) {
        try {
            Product product = productService.getProductById(id)
                    .orElseThrow(() -> new ResourceNotFoundException("产品不存在，ID: " + id));
            return ResponseResult.success("获取产品成功", product);
        } catch (ResourceNotFoundException e) {
            return ResponseResult.error(404, e.getMessage());
        }
    }
    
    // 根据产品代码获取产品
    @GetMapping("/code/{productCode}")
    public ResponseResult<Product> getProductByCode(@PathVariable String productCode) {
        try {
            Product product = productService.getProductByCode(productCode)
                    .orElseThrow(() -> new ResourceNotFoundException("产品不存在，代码: " + productCode));
            return ResponseResult.success("获取产品成功", product);
        } catch (ResourceNotFoundException e) {
            return ResponseResult.error(404, e.getMessage());
        }
    }
    
    // 添加产品
    @PostMapping
    public ResponseResult<Product> addProduct(@RequestBody Product product) {
        try {
            Product newProduct = productService.addProduct(product);
            return ResponseResult.success("添加产品成功", newProduct);
        } catch (RuntimeException e) {
            return ResponseResult.error(400, "添加产品失败: " + e.getMessage());
        }
    }
    
    // 更新产品
    @PutMapping("/{id}")
    public ResponseResult<Product> updateProduct(@PathVariable Long id, @RequestBody Product product) {
        try {
            Product updatedProduct = productService.updateProduct(id, product);
            return ResponseResult.success("更新产品成功", updatedProduct);
        } catch (RuntimeException e) {
            return ResponseResult.error(404, "更新产品失败: " + e.getMessage());
        }
    }
    
    // 删除产品
    @DeleteMapping("/{id}")
    public ResponseResult<Void> deleteProduct(@PathVariable Long id) {
        try {
            productService.deleteProduct(id);
            return ResponseResult.success("删除产品成功", null);
        } catch (RuntimeException e) {
            return ResponseResult.error(404, "删除产品失败: " + e.getMessage());
        }
    }
    
    // 根据分类获取产品
    @GetMapping("/category/{category}")
    public ResponseResult<List<Product>> getProductsByCategory(@PathVariable String category) {
        List<Product> products = productService.getProductsByCategory(category);
        return ResponseResult.success("获取分类产品列表成功", products);
    }
    
    // 搜索产品名称
    @GetMapping("/search")
    public ResponseResult<List<Product>> searchProductsByName(@RequestParam String keyword) {
        List<Product> products = productService.searchProductsByName(keyword);
        return ResponseResult.success("搜索产品成功", products);
    }
}